<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="projectId">
        <el-select v-model="queryParams.projectId" placeholder="请选择项目" clearable style="width: 300px;">
          <el-option
            v-for="dict in project_list"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择工单类型" clearable style="width: 150px;">
          <el-option
            v-for="dict in selected.order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['mes:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column v-if="false" label="主键ID" align="center" prop="orderId"/>
      <el-table-column label="项目名称" align="center" prop="projectId">
        <template #default="scope">
          <dict-tag :options="project_list" :value="scope.row.projectId"/>
        </template>
      </el-table-column>
      <el-table-column label="取货司机" align="center" prop="driver">
        <template #default="scope">
          <dict-tag :options="driverList" :value="scope.row.driver"/>
        </template>
      </el-table-column>
      <el-table-column label="货运方式" align="center" prop="transportType">
        <template #default="scope">
          <dict-tag :options="transport_type" :value="scope.row.transportType"/>
        </template>
      </el-table-column>
      <el-table-column label="实际用车" align="center" prop="factLicensePlate">
        <template #default="scope">
          <dict-tag :options="car_list" :value="scope.row.factLicensePlate"/>
        </template>
      </el-table-column>
      <el-table-column label="工单类型" align="center" prop="orderType">
        <template #default="scope">
          <dict-tag :options="selected.order_type" :value="scope.row.orderType"/>
        </template>
      </el-table-column>
      <el-table-column label="工单状态" align="center" prop="orderStatus">
        <template #default="scope">
          <dict-tag :options="order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="updateTime" width="200"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  width="300">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleDetail(scope.row)" v-hasPermi="['mes:order:edit']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 详情 -->
    <el-dialog :title="title" v-model="detail" width="1000px" append-to-body>
      <el-form ref="orderRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12" v-if="form.projectId">
            <el-form-item label="项目名称" prop="projectId">
              <el-select v-model="form.projectId" placeholder="请选择项目" style="width: 300px;" disabled>
                <el-option
                  v-for="dict in project_list"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>            
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人" prop="responsiblePerson">
              <el-input v-model="form.responsiblePerson" placeholder="请输入"  style="width: 300px;" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="companyName">
              <el-input v-model="form.companyName" placeholder="请输入"  style="width: 300px;" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" prop="companyPerson">
              <el-input v-model="form.companyPerson" placeholder="请输入"  style="width: 300px;" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人电话" prop="companyPhone">
              <el-input v-model="form.companyPhone" placeholder="请输入"  style="width: 300px;" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="取货司机" prop="driver">
              <el-select v-model="form.driver" placeholder="请输入" style="width: 300px;" disabled>
                <el-option
                  v-for="dict in driverList"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>            
          </el-col>
          <el-col :span="12">
            <el-form-item label="货运方式" prop="transportType">
              <el-select v-model="form.transportType" placeholder="请选择货运方式"  style="width: 300px;" disabled>
                <el-option
                  v-for="dict in transport_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.bookLicensePlate">
            <el-form-item label="预定用车" prop="bookLicensePlate">
              <el-select v-model="form.bookLicensePlate" placeholder="请选择预定用车"  style="width: 300px;" disabled>
                <el-option
                  v-for="dict in car_list"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.factLicensePlate">
            <el-form-item label="实际用车" prop="factLicensePlate">
              <el-select v-model="form.factLicensePlate" placeholder="请选择实际用车"  style="width: 300px;" disabled>
                <el-option
                  v-for="dict in car_list"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.takeCarDistance">
            <el-form-item label="取车公里数" prop="takeCarDistance">
              <el-input v-model="form.takeCarDistance" placeholder="请输入"  style="width: 300px;" type="number" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工单类型" prop="orderType">
              <el-select v-model="form.orderType" placeholder="请选择工单类型"  style="width: 300px;" disabled>
                <el-option
                  v-for="dict in selected.order_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.takeCarTime">
            <el-form-item label="取车时间" prop="takeCarTime">
              <el-date-picker clearable  style="width: 300px;" disabled
                v-model="form.takeCarTime"
                type="datetime"
                value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="请选择取车时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.departTime">
            <el-form-item label="出发时间" prop="departTime">
              <el-date-picker clearable   style="width: 300px;" disabled
                v-model="form.departTime"
                type="datetime"
                value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="请选择出发时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.arriveTime">
            <el-form-item label="到达时间" prop="arriveTime">
              <el-date-picker clearable   style="width: 300px;" disabled
                v-model="form.arriveTime"
                type="datetime"
                value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="请选择到达时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.backTime">
            <el-form-item label="返程时间" prop="backTime">
              <el-date-picker clearable   style="width: 300px;" disabled
                v-model="form.backTime"
                type="datetime"
                value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="请选择返程时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.backStoreTime">
            <el-form-item label="到厂时间" prop="backStoreTime">
              <el-date-picker clearable   style="width: 300px;" disabled
                v-model="form.backStoreTime"
                type="datetime"
                value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="请选择到厂时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.inputStoreTime">
            <el-form-item label="入库时间" prop="inputStoreTime">
              <el-date-picker clearable   style="width: 300px;" disabled
                v-model="form.inputStoreTime"
                type="datetime"
                value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="请选择入库时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.returnCarDistance">
            <el-form-item label="还车公里数" prop="returnCarDistance">
              <el-input v-model="form.returnCarDistance" placeholder="请输入"  style="width: 300px;" type="number" :disabled="form.orderStatus != 7" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.returnCarTime">
            <el-form-item label="还车时间" prop="returnCarTime">
              <el-date-picker clearable   style="width: 300px;" disabled
                v-model="form.returnCarTime"
                type="datetime"
                value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="请选择还车时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="center">单据图片</el-divider>
        <el-form-item label="图片" prop="imageOrder" disabled>
          <image-upload v-model="form.imageOrder"/>
        </el-form-item>
        <el-divider content-position="center">货物图片</el-divider>
        <el-form-item label="图片" prop="imageCargo" disabled>
          <image-upload v-model="form.imageCargo"/>
        </el-form-item>
        <div v-if="form.transportType == 1">
          <el-divider content-position="center">车辆图片</el-divider>
          <el-form-item label="图片" prop="imageCar" disabled>
            <image-upload v-model="form.imageCar"/>
          </el-form-item>
        </div>
        <el-divider content-position="center">工单货物信息</el-divider>
        <div v-if="[1, 2].includes(form.orderType)">
          <el-table :data="storeOrderCargoList" :row-class-name="rowStoreOrderCargoIndex" @selection-change="handleStoreOrderCargoSelectionChange" ref="storeOrderCargo" style="width: 100%">
            <el-table-column v-if="false" type="selection" width="100" align="center" />
            <el-table-column v-if="false" label="序号" align="center" prop="index" width="50"/>
            <el-table-column label="产品名称" align="center"  prop="cargoName" :min-width="200">
              <template #default="scope">
                <el-select v-model="scope.row.cargoName" placeholder="请选择" disabled>
                  <el-option
                    v-for="dict in projectCargo_list"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.label"
                  ></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="数量" align="center"  prop="cargoNum" :min-width="120">
              <template #default="scope">
                <el-input v-model="scope.row.cargoNum" placeholder="请输入整数" disabled/>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div v-if="[3, 4].includes(form.orderType)">
          <el-table :data="storeOrderAuxiliaryList" ref="storeOrderAuxiliary">
            <el-table-column label="类别" align="center" prop="unit" width="120">
              <template #default="scope">
                <el-select v-model="scope.row.auxiliaryType" placeholder="请输入类别" clearable disabled>
                  <el-option
                    v-for="dict in auxiliary_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="辅料名称" align="center" prop="name" width="230">
              <template #default="scope">
                <el-input v-model="scope.row.name" placeholder="请输入辅料名称" disabled/>
              </template>
            </el-table-column>
            <el-table-column label="型号" align="center" prop="model" width="200">
              <template #default="scope">
                <el-input v-model="scope.row.model" placeholder="请输入型号" disabled/>
              </template>
            </el-table-column>
            <el-table-column label="规格" align="center" prop="specifications" width="120">
              <template #default="scope">
                <el-input v-model="scope.row.specifications" placeholder="请输入规格" disabled/>
              </template>
            </el-table-column>
            <el-table-column label="单位" align="center" prop="unit" width="120">
              <template #default="scope">
                <el-input v-model="scope.row.unit" placeholder="请输入单位" disabled/>
              </template>
            </el-table-column>
            <el-table-column label="数量" align="center" prop="num" width="180">
              <template #default="scope">
                <el-input-number v-model="scope.row.num" placeholder="请输入数量" min="1" disabled/>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Order">
import { listOrder, getOrderDetail} from "@/api/mes/order";
import { getProjectAll } from "@/api/mes/project";
import { listCar } from "@/api/mes/car";

const { proxy } = getCurrentInstance();
const { transport_type ,order_status ,auxiliary_type } = proxy.useDict( 'transport_type' , 'order_status' , 'auxiliary_type');
const { driverList } = proxy.mesDriverList("driverList");


const open = ref(false);
const detail = ref(false);

const orderList = ref([]);
const storeOrderCargoList = ref([]);
const storeOrderAuxiliaryList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedStoreOrderCargo = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const project_list = ref([]);
const car_list = ref([]);
const projectCargo_list = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: null,
    orderType: null,
    orderWholeStatus: '1',
  },
  rules: {
  },
  selected: {
    order_type: [
      { value: '1', label: '货物入库' },
      { value: '2', label: '货物出库' },
      { value: '3', label: '辅料入库' },
      { value: '4', label: '辅料出库' }
    ]
  }
});

const { queryParams, form, rules , selected } = toRefs(data);

// 表单重置
function reset() {
  form.value = {
    orderId: null,
    orderBatch: null,
    projectId: null,
    contactPerson: null,
    driver: null,
    transportTime: null,
    responsiblePerson: null,
    transportType: null,
    bookLicensePlate: null,
    orderType: null,
    storePerson: null,
    orderStatus: null,
    takeCarTime: null,
    takeCarDistance: null,
    factLicensePlate: null,
    departTime: null,
    arriveTime: null,
    backTime: null,
    backStoreTime: null,
    inputStoreTime: null,
    productCheck: null,
    productDesc: null,
    returnCarTime: null,
    returnCarStatus: null,
    returnCarDistance: null,
    createTime: null,
    updateTime: null,
    remark: null,
    status: null,
    cargo:{},
    companyName: null,
    companyPerson: null,
    companyPhone: null,
    imageCargo: null,
    imageCar: null,
    imageOrder: null
  };
  storeOrderCargoList.value = [];
  storeOrderAuxiliaryList.value = [];
  proxy.resetForm("orderRef");
}

/** 查询工单管理列表 */
function getList() {
  loading.value = true;
  listOrder(queryParams.value).then(response => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询项目管理 */
function getProjectList() {
  getProjectAll().then(response => {
    project_list.value = response.rows.map(project => ({
          value: project.projectId.toString(),
          label: project.projectName
        }));
  });
}

/** 查询车辆列表 */
function getCarList() {
  listCar().then(response => {
    car_list.value = response.data.map(car => ({
          value: car.carId.toString(),
          label: car.carNo
        }));
  });
}


// 取消按钮
function cancel() {
  detail.value = false;
  open.value = false;
  reset();
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.orderId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

function handleDetail(row) {
  reset();
  getOrderDetail({orderId:row.orderId,orderType:row.orderType}).then(response => {
    form.value = response.data;
    if(response.data.storeOrderCargoList){
      storeOrderCargoList.value = response.data.storeOrderCargoList;
    }
    if(response.data.storeOrderAuxiliaryList){
      storeOrderAuxiliaryList.value = response.data.storeOrderAuxiliaryList
    }
    detail.value = true;
    title.value = "详情";
  });
}

/** 工单货物序号 */
function rowStoreOrderCargoIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 复选框选中数据 */
function handleStoreOrderCargoSelectionChange(selection) {
  checkedStoreOrderCargo.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('mes/order/export', {
    ...queryParams.value
  }, `order_${new Date().getTime()}.xlsx`)
}

getList();
getProjectList();
getCarList();
</script>
